package com.chat;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.chat.entity.DataPackage;
import com.chat.entity.ServerAction;
import com.chat.entity.User;
import com.chat.entity.event.ChannelEvent;
import com.chat.net.SocketClient;

import java.sql.Date;
import java.util.Calendar;

public class RegisterActivity extends AppCompatActivity {

    EditText user,pass,againpass;
    Button btn;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        init();

    }


    public void init(){
        //handler
        final Handler handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                if(msg.what==200){
                    Toast.makeText(RegisterActivity.this, "注册成功!", Toast.LENGTH_SHORT).show();
                    finish();
                }
                else{
                    Toast.makeText(RegisterActivity.this,"注册失败,请再次尝试!",Toast.LENGTH_SHORT).show();
                }
            }
        };

        user = findViewById(R.id.reg_name);
        pass = findViewById(R.id.reg_pass);
        againpass = findViewById(R.id.reg_again_pass);
        btn = findViewById(R.id.reg_btn);

        //注册按钮点击事件
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String account = user.getText().toString();
                String strPass = pass.getText().toString();
                String strAgainPass = againpass.getText().toString();

                //注册信息非空验证
                if(strAgainPass.equals(strPass)&&!account.equals("")){
                    //注册用户信息
                    User user = new User();
                    user.setAccount(account);
                    user.setPassword(strPass);
                    user.setRose("游客");
                    user.setName("用户啦");
                    user.setRegDate(new Date(Calendar.getInstance().getTimeInMillis()).toString());
                    //将注册信息封装到数据包中
                    DataPackage dataPackage = DataPackage.request(user, ServerAction.REGISTER);
                    //发送数据包
//                    ChatObjectServer.getServer().addObject(dataPackage);
                    //添加channel事件，当数据包返回时回调onChannelEvent方法
                    SocketClient.getSocketClient().addChannelEvent(new ChannelEvent() {
//                        @Override
//                        public void onChannelEvent(ChannelHandlerContext ctx, DataPackage source) {
//                            //Handler 更新
//                            Message message = Message.obtain();
//                            message.what = source.getCode();
//                            handler.sendMessage(message);
//                        }
                    },dataPackage);
                }

            }
        });
    }
}